<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: Teacher
  Date: 2023/11/23
  Time: 10:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JSTL表达式</title>
</head>
<body>
    <h4>if逻辑</h4>
    <%
        boolean flag = false;
        request.setAttribute("flag",flag);
    %>
    <%--
        jstl 只能获取HttpServletRequest对象中的定义属性
     --%>
    <c:if test="${flag}">
        <p style="color:darkblue">flag为true</p>
    </c:if>
    <c:if test="${!flag}">
        <p style="color: brown">flag为false</p>
    </c:if>

    <h4>choose逻辑-if的多分支逻辑</h4>
    <% request.setAttribute("num",30); %>
    <p>
        <c:choose>
            <c:when test="${num>50}">大于50</c:when>
            <c:when test="${num==50}">等于50</c:when>
            <c:otherwise>小于50</c:otherwise>
        </c:choose>
    </p>

    <h4>forEach逻辑</h4>
    <%
        ArrayList<String> names = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            names.add("tom"+i);
        }
        request.setAttribute("names",names);
    %>
    <ul>
        <%--
            items 定义循环数据
            var 声明循环过程的值对应的变量名
            varStatus 声明循环过程中可用的对应值
                + index 下标
                + count 下标+1
                + current 循环对应的元素
        --%>
        <c:forEach items="${names}" var="name" varStatus="status">
            <li>${ status.index } = ${ name }</li>
        </c:forEach>
    </ul>
    <%
        HashMap<String,String> map = new HashMap<>();
        for (int i = 0; i < 8; i++) {
            map.put("k"+i,"jack"+i);
        }
        request.setAttribute("map",map);
    %>
    <ul>
        <c:forEach items="${map}" var="entry">
            <li>${ entry } : key=${entry.key} : value=${entry.value}</li>
        </c:forEach>
    </ul>
    <h4>日期格式化</h4>
    <% request.setAttribute("day",new Date()); %>
    <p>${ day }</p>
    <p>
        <fmt:formatDate value="${day}" pattern="yyyy-MM-dd HH:mm:ss" var="aday"></fmt:formatDate>
        <hr>
        <span>${ aday }</span>
    </p>
    <p>
        <fmt:formatDate value="${day}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>
    </p>
</body>
</html>
